package com.dart.sys.mq.produer.queue;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

import com.dart.sys.entity.SysOperateLog;

@Component
public class TestQueueSender {
	

	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate ;
	
	public void sendText(final String text) {
		try {
			this.jmsTemplate.send("test.queue.text", new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					System.out.println("发送Queue消息....");
					TextMessage message = session.createTextMessage() ;
					
					message.setText(text);
					
					return message;
				}
			});
		} catch (JmsException e) {
			System.out.println("异常");
		}
	}
	
	public void sendMap(Map<String, Object> map) {
		try {
			this.jmsTemplate.convertAndSend("test.queue.map", map, new MessagePostProcessor() {
				@Override
				public Message postProcessMessage(Message message) throws JMSException {
					return message;
				}
			});
		} catch (JmsException e) {
			System.out.println("异常");
		}
	}
	
	public void sendObject(final SysOperateLog entity) {
		try {
			
			this.jmsTemplate.send("test.queue.object", new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					ObjectMessage message = session.createObjectMessage();
					message.setObject(entity);
					return message;
				}
			});
			
		} catch (JmsException e) {
			System.out.println("异常");
		}
	}
	
}
